from database.db import Database


def init_database(db_name:str = None):
    db = Database(db_name)
    fields_list = [
        {'name':'id',             'type':'INTEGER PRIMARY KEY AUTOINCREMENT'},  
        {'name':'name',           'type':'VARCHAR(255)'},  
        {'name':'supplier',       'type':'VARCHAR(255)'},  
        {'name':'model',          'type':'VARCHAR(255)'},  
        {'name':'position',       'type':'VARCHAR(255)'},  
        {'name':'ip',             'type':'VARCHAR(255)'},  
        {'name':'port',           'type':'VARCHAR(255)'},  
        {'name':'user',           'type':'VARCHAR(255)'},  
        {'name':'password',       'type':'VARCHAR(255)'},  
        {'name':'info',           'type':'TEXT'},  
        {'name':'created_time',   'type':'TEXT'},  
        {'name':'updated_time',   'type':'TEXT'},  
    ]
    db.create_table('devices',fields_list)

    fields_list = [
        {'name':'id',             'type':'INTEGER PRIMARY KEY AUTOINCREMENT'},  
        {'name':'name',           'type':'VARCHAR(255)'},  
        {'name':'content',        'type':'TEXT'},  
        {'name':'textfsm',        'type':'TEXT'},  
        {'name':'info',           'type':'TEXT'},  
        {'name':'type',           'type':'TEXT'},  
        {'name':'group_name',     'type':'TEXT'},  
    ]

    db.create_table('scripts',fields_list)

    fields_list = [
        {'name':'id',             'type':'INTEGER PRIMARY KEY AUTOINCREMENT'},  
        {'name':'name',           'type':'VARCHAR(255)'},  
    ]
    db.create_table('script_group',fields_list)

    fields_list = [
        {'name':'id',             'type':'INTEGER PRIMARY KEY AUTOINCREMENT'},  
        {'name':'script_id',      'type':'INTEGER'},  
        {'name':'device_id',      'type':'INTEGER'},  
    ]
    foreign_list = [
        {'key':'script_id','ref':'scripts (id)'},
        {'key':'device_id','ref':'devices (id)'},
    ]
    db.create_table('private_script_device',fields_list,foreign_list)
    
    fields_list = [
        {'name':'id',             'type':'INTEGER PRIMARY KEY AUTOINCREMENT'},  
        {'name':'script_id',      'type':'INTEGER'},  
        {'name':'group_id',      'type':'INTEGER'},  
    ]
    foreign_list = [
        {'key':'script_id','ref':'scripts (id)'},
        {'key':'group_id','ref':'script_group (id)'},
    ]
    db.create_table('public_script_group',fields_list,foreign_list)
    
    fields_list = [
        {'name':'id',             'type':'INTEGER PRIMARY KEY AUTOINCREMENT'},  
        {'name':'name',           'type':'VARCHAR(255)'},  
        {'name':'device_id',      'type':'INTEGER'},  
        {'name':'script_id',      'type':'INTEGER'},  
        {'name':'start_time',     'type':'TEXT'},  
        {'name':'exec_times',     'type':'INTEGER'},  
        {'name':'loop_day',       'type':'INTEGER'},  
        {'name':'loop_hour',      'type':'INTEGER'},  
    ]
    foreign_list = [
        {'key':'device_id','ref':'devices (id)'},
        {'key':'script_id','ref':'scripts (id)'},
    ]
    db.create_table('tasks',fields_list,foreign_list)
